package com.sinobpo.rnd2.ehr.web.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sinobpo.rnd2.ehr.model.User;
import com.sinobpo.rnd2.ehr.model.UserActionForm;
import com.sinobpo.rnd2.ehr.util.UConst;

/**
 * 
 * @author weida.ni
 * 
 */
public class BaseAction extends ActionSupport {
	ActionContext     			context = ActionContext.getContext();
	public 	HttpServletRequest 	request;
	public 	HttpServletResponse response;
	public 	SessionMap 			session;
	
	public  User                oldUser; 
	public 	Map 				responseJson;
	private JSONObject 			message; 
	
	@Resource
	public SimpleJdbcTemplate simpleJdbcTemplate;
	private List write;// json返回是否成功

	public BaseAction(){
		
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) context
				.get(ServletActionContext.HTTP_RESPONSE);
		Map session = context.getSession();

		setServletRequest(request);
		setServletResponse(response);
		setSession(session);
		try{
			//判断有无登录
			oldUser = (User) session.get(UConst.LOGON);
			if(oldUser==null &&(this.getClass()!=LogonAction.class)){
				//跳转到登陆页面
				System.out.println("跳转到登陆页面");
				RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
			    rd.forward(request,response);
	
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	//保存用户
	public void saveUsers(User user){
		if(user!=null)
			session.put(UConst.LOGON, user);
	}
	//注销用户
	public void reset(){
		session.put(UConst.LOGON, null);
	}
	public void setSession(Map map) {
		this.session = (SessionMap) map;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	public List getWrite() {
		return write;
	}

	public void setWrite(List write) {
		this.write = write;
	}

	public Map getResponseJson() {
		return responseJson;
	}

	public void setResponseJson(Map responseJson) {
		this.responseJson = responseJson;
	}

	public JSONObject getMessage() {
		return message;
	}

	public void setMessage(JSONObject message) {
		this.message = message;
	}

}
